import axios from 'axios'

// 创建实例
const instance = axios.create({
    timeout: 15000
})

// 请求拦截

instance.interceptors.request.use(config => {
    // 设置请求头
    if (localStorage.getItem('token')) {
        config.headers['X-Nideshop-Token'] = localStorage.getItem('token')
    }
    return config
}, error => {
    return Promise.reject(error)
})


// 响应拦截

instance.interceptors.response.use(result => {
    if (result.data.errno === 1000) {
        localStorage.clear();
        window.location.reload()
    }
    return result.data
}, error => {
    return Promise.reject(error)
})

export default instance